﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GaugeEngine.Process
{
    public class ProcessManager
    {
        private List<IProcess> m_processes = new List<IProcess>();

        public ProcessManager(Game g)
        {

        }

        public void attachProcess(IProcess proc)
        {
            m_processes.Add(proc);
        }

        public void removeProcess(IProcess proc)
        {
            m_processes.Remove(proc);
        }

        public void Update(GameTime dt)
        {
            List<IProcess> dead_processes = new List<IProcess>();
            List<IProcess> new_processes = new List<IProcess>();
            foreach (IProcess proc in m_processes)
            {
                proc.onUpdate(dt);
                if (proc.Finished())
                {
                    dead_processes.Add(proc);
                    if (proc.Next != null) new_processes.Add(proc.Next);
                }
            }

            foreach (IProcess dead_proc in dead_processes)
            {
                m_processes.Remove(dead_proc);
            }

            foreach (IProcess new_proc in new_processes)
            {
                attachProcess(new_proc);
            }
        }
    }
}
